#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :下载图片.py
# @Time      :2024/7/19 
# @Author    :CL
# @email     :1037654919@qq.com
import datetime
import os
import re
import time
import requests
from retrying import retry
from util import mongo_manager
from fanyi import fanyi
dongchedi_images_info = mongo_manager('dongchedi_images_info', db='car_images')
dongchedi_serires_id = mongo_manager('dongchedi_serires_id', db='car_images')
proxies = {
        'http': 'http://127.0.0.1:15732',
        'https': 'http://127.0.0.1:15732'
    }
# 下载图片
@retry(stop_max_attempt_number=3)
def download_picture(url, file):
    if not url.endswith('webp'):
        return 'no webp'
    headers = {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "priority": "u=0, i",
        "referer": "https://www.dongchedi.com/",
        "sec-ch-ua": "\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "cross-site",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
    }
    response = requests.get(url,headers=headers,proxies = proxies, timeout=10)
    print(response.url, response)
    if response.status_code == 200:
        with open(file, 'wb') as f:
            f.write(response.content)
        return 'done'
    return 'error'
def save_txt(txt,file):
    with open(file,'w',encoding='utf-8') as f:
        f.write(txt)


def sanitize_pathname(pathname):
    # 定义不合法的字符集合
    illegal_chars = r'[<>:"\\|?*\x00-\x1F\x7F]' # /不要替换
    # 使用正则表达式替换不合法字符为下划线
    sanitized_pathname = re.sub(illegal_chars, '_', pathname.strip())
    return sanitized_pathname
def main():
    # 先跑新能源车 理想 问界 蔚来 极氪 零跑 深蓝 小米 小鹏 腾势 智己
    keywords = ["理想", "问界", "蔚来", "极氪", "零跑", "深蓝", "小米", "小鹏", "腾势", "智己"]
    # 构建查询条件
    query = {
        'car_series': {'$regex': '|'.join(keywords)}
    }
    seed = dongchedi_serires_id.findAll(query)
    for s in seed:
        while True:
            seed = dongchedi_images_info.find_one_and_update(
                {'status':'nodown','webp_urls':{"$exists":True},'year':{"$exists":True},'series_id':s['code']},
                {'status': "downloading"})
            if seed:
                print(seed['base_url'],seed['car_style'],seed['car_name'],seed['lens'])
                path = f"/media/chenglei3/77D014CED257D1411/images/dongchedi_images_xny/{seed['category']}/{seed['car_style']}/{seed['car_name'].strip()}/"
                path = sanitize_pathname(path)
                os.makedirs(path, exist_ok=True)
                txts=[]
                if len(seed['colors'])>0:
                        for color,code in seed['colors'].items():
                            path = f"/media/chenglei3/77D014CED257D1411/images/dongchedi_images_xny/{seed['category']}/{seed['car_style']}/{seed['car_name'].strip()}/{color.replace('/','_')}/"
                            path = sanitize_pathname(path)
                            os.makedirs(path, exist_ok=True)
                            txt = f"{seed['car_style']}, {seed['car_name']},{color.replace('/', '_')},year {seed['year']}"
                            txt_en  = fanyi(txt)
                            txts.append([str(code).rsplit('_',1)[0],txt,txt_en])
                            for url, webp_url in seed['webp_urls'].items():
                                if str(code).rsplit('_',1)[0] in url:
                                    reslut = download_picture(webp_url,  f"{path}{webp_url.split('/')[-1]}")
                                    if reslut == 'done':
                                        save_txt(txt_en,f"{path}{webp_url.split('/')[-1].replace('.webp','.txt')}")
                else:
                    txt = f"{seed['car_style']}, {seed['car_name']},year {seed['year']}"
                    txt_en = fanyi(txt)
                    txts.append(['x', txt, txt_en]) # x代表无颜色
                    for url, webp_url in seed['webp_urls'].items():
                        reslut = download_picture(webp_url, file = f"{path}{webp_url.split('/')[-1]}")
                        if reslut == 'done':
                            save_txt(txt_en, f"{path}{webp_url.split('/')[-1].replace('.webp', '.txt')}")
                dongchedi_images_info.updateOne({'_id':seed['_id']}, {'status': 'done','txts':txts})
            else:
                break

if __name__ == '__main__':
    print(datetime.datetime.now())
    dongchedi_images_info.updateMany({'status':'downloading'},  {'status': "nodown"})
    main()
